package com.ys.util.dhecp;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @ClassName DateUtils
 * @Description TODO
 * @Author alula
 * @Date 2022-09-24 11:31
 */
public class DateUtils {

    /**
     * 将带T类型的字符串时间转成正常时间
     * @param tDate 2018-04-20T22:59:59Z
     * @return
     */
    public static Date convertTDateToDate(String tDate){
        Date resultDate = null;
        if (StringUtils.isEmpty(tDate)){
            return  resultDate;
        }
        SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        try {
            resultDate = inputFormat.parse(tDate);
        }catch (Exception ex){
        }
        return  resultDate;
    }

    /**
     * 将带T类型的字符串时间转成正常时间2
     * @param tDate
     * @return
     */
    public static Date convertTDateToDate2(String tDate){
        Date resultDate = null;
        if (StringUtils.isEmpty(tDate)){
            return  resultDate;
        }
        SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        try {
            resultDate = inputFormat.parse(tDate);
        }catch (Exception ex){
        }
        return  resultDate;
    }

    /**
     * 将正常时间转成带T类型的字符串时间
     * @param date
     * @return
     */
    public static String convertDateToTDate(Date date){
        String resultStr = "";
        if (date == null){
            return  resultStr;
        }
        SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        try {
            resultStr = inputFormat.format(date);
        }catch (Exception ex){
        }
        return  resultStr;
    }

    /**
     * 时间根据格式化
     * @param date
     * @return
     */
    public static String convertDateToTDate(Date date,String format){
        String resultStr = "";
        if (date == null){
            return  resultStr;
        }
        SimpleDateFormat inputFormat = new SimpleDateFormat(format);
        try {
            resultStr = inputFormat.format(date);
        }catch (Exception ex){
        }
        return  resultStr;
    }

    /**
     * 格林尼治时间转date
     * @param dateStr
     * @return
     */
    public  static  Date convertGreenwichDateToData(String dateStr){
        Date resultDate = null;
        if (StringUtils.isEmpty(dateStr)){
            return  resultDate;
        }
        SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
        try {
            resultDate = inputFormat.parse(dateStr);
        }catch (Exception ex){
        }
        return  resultDate;
    }

    /**
     * 根据dateStr 获取时间
     * @param dateStr 查询的天数
     * @param millisecond  毫秒
     * @param second  秒
     * @param minute 分
     * @param hour 时
     * @param hour_of_day 默认0
     * @return
     */
    public  static  Date firstAndLateData(int dateStr, int millisecond, int second, int minute, int hour, int hour_of_day, int type, Date paymentTime){
        Calendar calendar = new GregorianCalendar();
        if (type == 0){
            calendar.add(Calendar.DATE, dateStr);
        } else if (type == 1){
            calendar.setTime(paymentTime);
        }
        calendar.set(Calendar.MILLISECOND, millisecond);
        calendar.set(Calendar.SECOND, second);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.HOUR, hour);
        calendar.set(Calendar.HOUR_OF_DAY, hour_of_day);
        return  calendar.getTime();
    }
}
